import axios from 'axios'
import {
  Notify
} from 'vant'
import router from '../router'
import store from '@/store'
import qs from 'qs'

// const baseUrl = store.state.apiUrl
let baseUrl = "";
switch (process.env.NODE_ENV) {
  case "development": //开发环境
    // baseUrl = "/proxy";
    // baseUrl = "http://36.137.26.248:8083/";
    baseUrl = "https://wsgdqx.wzheiying.com:8083/";
    break;
  case "test": //测试环境
    // baseUrl = "http://36.137.26.248:8083/"; //正式
    baseUrl = "";//测试
    break;
  case "production":
    // baseUrl = "https://api.xiangcun0577.com/";//生产环境 浙里办
    baseUrl = "";
    break;
  default:
    baseUrl = "";
}
const rightCode = 200 //正确返回code
const failCode = 401 //未登录返回code
function isLogin() { //未登录
  var currentPath = router.history.current.path
  if (currentPath == '/'||currentPath == '/login') {
    return
  }
  window.localStorage.removeItem('userList')
  Notify({
    type: 'danger',
    message: '登录超时'
  });
  console.log(currentPath,currentPath.indexOf('wxh5'))
  if (currentPath.indexOf('wxh5') != -1) {
    router.replace({
      path: '/login?currentPath=' + currentPath
    })
  } else {

    router.replace({
      path: '/?currentPath=' + currentPath
    })
  }
}
const instance = axios.create({
  baseURL: baseUrl,
  timeout: 10000,
  // withCredentials: true
})
const instance2 = axios.create({
  baseURL: baseUrl,
  timeout: 10000,
  withCredentials: false,
  headers: {
    "Content-Type": 'application/json'
  }
})
const instance3 = axios.create({
  baseURL: baseUrl,
  timeout: 10000,
  responseType: "blob",
  headers: {
    "Content-Type": 'application/x-www-form-urlencoded; charset=UTF-8'
  }
})

function changeLoading(bl) { //开关加载动画
  // store.state.loadingShow = bl
}



instance.interceptors.request.use(config => {
  try {
    // var Authorization = store.state.token ? store.state.token : ''
    var Authorization = window.localStorage.getItem('token')
    if (Authorization) {
      config.headers.common['Authorization'] = 'Bearer ' + Authorization;
    }
  } catch (e) {
    throw e
  }
  return config
}, err => {
  changeLoading(false) //关闭加载动画
  Notify.error('请求超时');
  return Promise.reject(err);
})
instance2.interceptors.request.use(config => {
  try {
    // var Authorization = store.state.token ? store.state.token : ''
    var Authorization = window.localStorage.getItem('token')
    if (Authorization) {
      config.headers.common['Authorization'] = 'Bearer ' + Authorization;
    }
  } catch (e) {
    throw e
  }
  return config
})
instance3.interceptors.request.use(config => {
  try {
    // var Authorization = store.state.token ? store.state.token : ''
    var Authorization = window.localStorage.getItem('token')
    if (Authorization) {
      config.headers.common['Authorization'] = 'Bearer ' + Authorization;
    }
  } catch (e) {
    throw e
  }
  return config
})

instance.interceptors.response.use(
  response => {
    changeLoading(false) //关闭加载动画
    if (response.data instanceof Blob) {
      return Promise.resolve(response)
    }
    if (response.data.code == rightCode) { //返回值正确返回值

      return response.data
    } else if (response.data.code == failCode) {
      Notify({

        message: response.data.msg,
        type: 'danger'
      })
      isLogin()
      return response.data
    } else { //如果报错  提示错误信息
      Notify({

        message: response.data.msg,
        type: 'danger'
      })
      return response.data
    }
  },
  error => {
    changeLoading(false) //关闭加载动画
    console.log('err', error)
    return Promise.reject(error)
  }
)
instance2.interceptors.response.use(
  response => {
    changeLoading(false) //关闭加载动画
    if (response.data.code == rightCode) { //返回值正确返回值
      return response.data
    } else if (response.data.code == failCode) {
      Notify({

        message: response.data.msg,
        type: 'danger'
      })
      isLogin()
      return response.data
    } else { //如果报错  提示错误信息
      Notify({

        message: response.data.msg,
        type: 'danger'
      })
      return response.data
    }
  },
  error => {
    changeLoading(false) //关闭加载动画
    console.log('err', error)
    return Promise.reject(error)
  }
)

export default {
  baseUrl,
  get(
    url,
    param,
    resParams = {
      message: true
    }
  ) {
    return new Promise((resolve, reject) => {
      instance
        .get(url, {
          params: param,
          resParams: resParams
        })
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  put(
    url,
    param,
    resParams = {
      message: true
    }
  ) {
    return new Promise((resolve, reject) => {
      instance
        .put(url, param)
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  delete(
    url,
    param,
    resParams = {
      message: true
    }
  ) {
    return new Promise((resolve, reject) => {
      instance
        .delete(url, {
          params: param,
          resParams: resParams
        })
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  post(
    url,
    param,
    resParams = {
      message: true
    },
    data = {
      loading: false
    }, //是否开启loading
  ) {
    if (data.loading == true) { //post接口传入{loading:true}
      changeLoading(true)
    }
    return new Promise((resolve, reject) => {
      instance
        .post(url, qs.stringify(param), {
          resParams: resParams
        })
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  //对象提交
  customPost(
    url,
    param,
    resParams = {
      message: true
    },
    // baseUrl = baseUrl,
  ) {
    changeLoading(true)
    return new Promise((resolve, reject) => {
      instance2
        .post(url, param, {
          resParams: resParams
        })
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  //导出
  downLoadGet(
    url,
    param,
    resParams = {
      message: true
    }
  ) {
    return new Promise((resolve, reject) => {
      instance3
        .post(url, qs.stringify(param)
          //    {
          //   params: param,
          //   resParams: resParams
          // }
        )
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
}